<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column as Col;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use app\common\model\ProductCate;
use think\facade\View;
use think\Response;

class Product extends Base
{

    public function getOptions(): array
    {
        return [
            'pk'       => 'id',
            'model'    => \app\common\model\Product::class,
            'with'     => ['cate1', 'cate2'],
            'validate' => \app\admin\validate\Product::class,
            'just'     => false,
            'false'    => [
                'delete' => 'productDelete'
            ]
        ];
    }


    public array $tape = [
        ['field' => 'icon', 'title' => '图片', 'type' => 'icon'],
        ['field' => 'price', 'title' => '原价'],
        ['field' => 'vip_price', 'title' => '会员价'],
        ['field' => 'stock', 'title' => '库存', 'type' => 'stock']
    ];

    public function index(): string
    {
        return Table::init($this)
            ->addColumns([
                ['id', '商品ID', fn(Col $col) => $col->width(80)],
                ['icon', '缩略图', fn(Col $col) => $col->width(80)->lazyImage()],
                ['title', '商品名称'],
                ['cate_1', '所属分类', fn(Col $col) => $col->templet('{{d.cate1.title}}  - {{d.cate2?d.cate2.title:"无"}}')->width(150)],
                ['price', '市场价', fn(Col $col) => $col->width(100)],
                ['vip_price', '会员价', fn(Col $col) => $col->width(100)],
                ['stock', '库存', fn(Col $col) => $col->width(100)],
                ['sales', '销量', fn(Col $col) => $col->width(100)],
                ['virtual_sales', '虚拟销量', fn(Col $col) => $col->width(100)],
                ['sort', '排序', fn(Col $col) => $col->fastSort()],
                ['pick', '推荐', fn(Col $col) => $col->switch('开启|关闭', '1|0')->fixed('right')],
                ['status', '状态', fn(Col $col) => $col->switch('开启|关闭', '1|0')->fixed('right')]
            ])
            ->addTopButtons(['add' => fn(Button $btn) => $btn->event('newPage'), 'deleteAll'])
            ->addRightButtons(['edit' => fn(Button $btn) => $btn->event('newPage')], 80)
            ->addSearch([
                ['title', '输入商品名称', fn(Search $s) => $s->condition('Like')],
                ['cate_1', '所属分类', fn($s) => $s->type('select')->optionValue(Product::getCate(2))],
            ])
            ->fetch();
    }


    public static function getCate($type = 1): array
    {

        return $type == 1 ?
            ProductCate::where('pid', 0)->field('id,title')->select()->toArray() :
            ProductCate::where('pid', 0)->column('title', 'id');
    }

    public function add(string $tpl = '')
    {
        View::assign('cate', self::getCate());
        View::assign('tape', $this->tape);
        return parent::add('product/edit');
    }

    public function edit(string $tpl = 'product/edit')
    {
        View::assign('cate', self::getCate());
        View::assign('tape', $this->tape);
        return parent::edit($tpl);
    }

    public function save(array $param = [], string $method = '_save'): Response
    {
        $param = $this->request->post();
        $param['tape'] = $this->tape;
        return parent::save($param, $method);
    }

}
